/*
*  This file is part of mangusta
*
*  mangusta is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  mangusta is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with mangusta; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
*
*  Author: <antonino.calderone@ericsson.com>, <acaldmail@gmail.com>
*
*/


/* -------------------------------------------------------------------------- */

#include "nu_lang_defs.h"
#include "nu_variant.h"
#include "nu_operators.h"
#include "nu_builtin_help.h"

#include <string>

/* -------------------------------------------------------------------------- */

namespace nu
{

/* -------------------------------------------------------------------------- */

global_operator_tbl_t* global_operator_tbl_t::_instance = nullptr;


/* -------------------------------------------------------------------------- */

global_operator_tbl_t& global_operator_tbl_t::get_instance()
{
   if (!_instance)
   {
      _instance = new global_operator_tbl_t();
      assert(_instance);

      global_operator_tbl_t & opmap = *_instance;
      using arg_t = const variant_t&;

      opmap[PROGLANG_OP_MOD] = [](arg_t a, arg_t b)
      {
         return a.int_mod(b);
      };
      opmap[PROGLANG_OP_LTEQ] = [](arg_t a, arg_t b)
      {
         return a <= b;
      };
      opmap[PROGLANG_OP_GTEQ] = [](arg_t a, arg_t b)
      {
         return a >= b;
      };
      opmap[PROGLANG_OP_EQUALS] = [](arg_t a, arg_t b)
      {
         return a == b;
      };
      opmap[PROGLANG_OP_NOTEQUALS] = [](arg_t a, arg_t b)
      {
         return a != b;
      };
      opmap[PROGLANG_OP_LT] = [](arg_t a, arg_t b)
      {
         return a < b;
      };
      opmap[PROGLANG_OP_GT] = [](arg_t a, arg_t b)
      {
         return a > b;
      };
      opmap[PROGLANG_OP_PLUS] = [](arg_t a, arg_t b)
      {
         return a + b;
      };
      opmap[PROGLANG_OP_MINUS] = [](arg_t a, arg_t b)
      {
         return a - b;
      };
      opmap[PROGLANG_OP_DIV] = [](arg_t a, arg_t b)
      {
         return a / b;
      };
      opmap[PROGLANG_OP_MUL] = [](arg_t a, arg_t b)
      {
         return a * b;
      };
      opmap[PROGLANG_OP_POWER] = [](arg_t a, arg_t b)
      {
         return a.power(b);
      };
      opmap[PROGLANG_OP_AND] = [](arg_t a, arg_t b)
      {
         return a && b;
      };
      opmap[PROGLANG_OP_OR] = [](arg_t a, arg_t b)
      {
         return a || b;
      };

      opmap[PROGLANG_OP_BOR] = [](arg_t a, arg_t b)
      {
         return a.to_int() | b.to_int();
      };
      opmap[PROGLANG_OP_BAND] = [](arg_t a, arg_t b)
      {
         return a.to_int() & b.to_int();
      };
      opmap[PROGLANG_OP_BXOR] = [](arg_t a, arg_t b)
      {
         return a.to_int() ^ b.to_int();
      };
      opmap[PROGLANG_OP_BSHR] = [](arg_t a, arg_t b)
      {
         return a.to_int() >> b.to_int();
      };
      opmap[PROGLANG_OP_BSHL] = [](arg_t a, arg_t b)
      {
         return a.to_int() << b.to_int();
      };

   }

   return *_instance;
}


/* -------------------------------------------------------------------------- */

} // nu

